package com.raja.requestForwarding.mq.channel;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.raja.requestForwarding.mq.connect.CustomConnection;
import com.raja.requestForwarding.mq.factory.RabbitMqFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/8/16/016.
 */
@Component
public class CustomChannel {
    private static final Logger log = LoggerFactory.getLogger(CustomChannel.class);

    private Channel channel = null;

    @Autowired
    private RabbitMqFactory rabbitMqFactory;
    @Autowired
    private CustomConnection customConnection;


    /**
     * 获得车辆的通道
     * @return
     * @throws Exception
     */
    public Channel getChannel(String queueName) {
        try {
            ConnectionFactory factory = rabbitMqFactory.init();
            if(factory != null){
                Connection  connection = customConnection.getConnection(factory);
                channel = connection.createChannel();
                channel.queueDeclare(queueName, true, false, false, null);
                channel.basicQos(1);
            }
            return channel;
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return null;
    }
}
